<?

class VenueList extends db {
	
	function get_total() {
		$q = 'select count(ID) as total from Venues';
		$r = mysql_query($q);
		if ($r) {
			$row = mysql_fetch_assoc($r);
			return $row['total'];
		} else {
			return Error::MySQL();
		}
	}
	
	function get($limiter) {
		$country = $limiter['country'] ? $limiter['country'] : VENUE_DEFAULT_COUNTRY;
		$stateProvince = $limiter['stateProvince'] ? $limiter['stateProvince'] : VENUE_DEFAULT_STATEPROVINCE;
		$city = $limiter['city'] ? $limiter['city'] : VENUE_DEFAULT_CITY;
		$specify_active = (isset($limiter['is_active'])) ? $limiter['is_active'] : false;
		
		$country = $this->sanitize_to_db($country);
		$stateProvince = $this->sanitize_to_db($stateProvince);
		$city = $this->sanitize_to_db($city);
		
		$query = "select ID from Venues where country = '{$country}' ";
		$query .= ($specify_active) ? "and is_active = {$is_active} " : "";
		$query .= "and stateProvince = '{$stateProvince}' ";
		if ($limiter['city'] || ($stateProvince == VENUE_DEFAULT_STATEPROVINCE)) {
			$query .= "and city = '{$city}' ";
		}
		$query .= "order by name asc";
			
		$r = mysql_query($query);
		if (!$r) {
			return Error::MySQL();
		}
		
		$venues = array();
		while ($row = mysql_fetch_assoc($r)) {
			$venues[] = Venue::get($row['ID']);
		}
		return $venues;
	}
	
	function getStateProvinceList($country = VENUE_DEFAULT_COUNTRY) {
		$country = $this->sanitize_to_db($country);
		$q = "select stateProvince, count(stateProvince) as total from Venues where country = '{$country}' group by stateProvince order by stateProvince asc";
		$r = mysql_query($q);
		if (!$r) {
			return Error::MySQL();
		}
		$splist = array();
		while ($row = mysql_fetch_assoc($r)) {
			$splist[$row['stateProvince']] = $row['total'];
		}
		return $splist;
	}
	
	function getCountryList() {
		$country = $this->sanitize_to_db($country);
		$q = "select country, count(country) as total from Venues group by country order by country asc";
		$r = mysql_query($q);
		if (!$r) {
			return Error::MySQL();
		}
		$clist = array();
		while ($row = mysql_fetch_assoc($r)) {
			$clist[$row['country']] = $row['total'];
		}
		return $clist;
	}
	
	function getCityList($limiter) {
		$country = $limiter['country'] ? $limiter['country'] : VENUE_DEFAULT_COUNTRY;
		$stateProvince = $limiter['stateProvince'] ? $limiter['stateProvince'] : VENUE_DEFAULT_STATEPROVINCE;

		$country = $this->sanitize_to_db($country);
		$stateProvince = $this->sanitize_to_db($stateProvince);
		$q = "select city, count(city) as total from Venues where country = '{$country}' and stateProvince = '{$stateProvince}' group by city order by city asc";
		$r = mysql_query($q);
		if (!$r) {
			return Error::MySQL();
		}
		$splist = array();
		while ($row = mysql_fetch_assoc($r)) {
			$splist[$row['city']] = $row['total'];
		}
		return $splist;
	}
	
}

class Venue extends db {

	var $ID, $name, $address1, $address2, $user_id, $city, $stateProvince, $postalCode, $directions, $is_active;
		
	function get($venueID) {
		if (is_numeric($venueID) && $venueID > 0) {
			$q = "select * from Venues where ID = " . $venueID;
			$db = new db;
			$r = mysql_query($q);
			if (!$r) {
				return Error::MySQL();
			} 
			
			$row = mysql_fetch_assoc($r);
			if ($row['ID']) {
				$ve = new Venue;
				$ve->ID = $row['ID'];		
				$ve->name = db::sanitize_from_db($row['name']);
				$ve->address1 = db::sanitize_from_db($row['address1']);
				$ve->address2 = db::sanitize_from_db($row['address2']);
				$ve->city = db::sanitize_from_db($row['city']);
				$ve->stateProvince = db::sanitize_from_db($row['stateProvince']);
				$ve->postalCode = $row['postalCode'];
				$ve->directions = db::sanitize_from_db($row['directions']);
				$ve->is_active = $row['is_active'];
				$ve->user_id = $row['user_id'];
				$ve->country = db::sanitize_from_db($row['country']);
				return $ve;
			}
			
		}
		
		return Error::create("Invalid venue ID.");
	}
	
	function getID() {return $this->ID;}
	function getName() {return $this->name;}
	function getAddress1() {return $this->address1;}
	function getAddress2() {return $this->address2;}
	function getCity() {return $this->city;}
	function getStateProvince() {return $this->stateProvince;}
	function getPostalCode() {return $this->postalCode;}
	function getCountry() {return $this->country;}
	function getCountryName() {
		$cl = new CountryList();
		if ($cl->countryList[$this->country] != null) {
			return $cl->countryList[$this->country];
		}
	}
	function getStateProvinceName() {
		$sl = new StateList();
		if ($sl->stateProvinceList[$this->stateProvince] != null) {
			return $sl->stateProvinceList[$this->stateProvince];
		} else {
			return $this->stateProvince;
		}
	}
	
	function getDirections() {return $this->directions;}
	function isActive() {return $this->is_active;}
	function getUserID() {return $this->user_id;}
	function getUserObject() {
		$uo = User::get($this->user_id);
		return $uo;
	}
		
	function canEdit() {
		$uo = User::getCurrent();		
		if (is_object($uo)) {
			return ($uo->getID() == $this->getUserID() || $uo->isAdmin());
		}
	}
	
	function canAdd() {
		$uo = User::getCurrent();		
		if (is_object($uo)) {
			return ($uo->isBandMember() || $uo->isAdmin());
		}
	}
	
	function activate() {
		if ($this->canEdit()) {
			$r = @mysql_query("update Venues set is_active = 1 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('You may not activate a venue you did not post.');
		}
	}
	
	function deactivate() {
		if ($this->canEdit()) {
			$r = @mysql_query("update Venues set is_active = 0 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('You may not deactivate a venue you did not post.');
		}
	}
	
	function remove() {
		if (User::isAdmin()) {
			$r = @mysql_query("delete from Venues where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may delete venues.');
		}
	}
	
	function update($postArray) { 
		// Admin user can update all posts, otherwise user can only update his own
		if (!$this->canEdit()) {
			return Error::create("You may not update a venue you did not post.");
		}
		$db = new db;
		$name = $db->sanitize_to_db($postArray['name']);
		$address1 = $db->sanitize_to_db($postArray['address1']);
		$address2 = $db->sanitize_to_db($postArray['address2']);
		$city = $db->sanitize_to_db($postArray['city']);
		$stateProvince = $db->sanitize_to_db($postArray['stateProvince']);
		$postalCode = $db->sanitize_to_db($postArray['postalCode']);
		$directions = $db->sanitize_to_db($postArray['directions']);
		
		$country = $db->sanitize_to_db($postArray['country']);
		$country = ($country == null) ? VENUE_DEFAULT_COUNTRY : $country;
		
		if ($stateProvince == "??") {
			$stateProvince = $db->sanitize_to_db($postArray['stateProvinceOther']);
		}
		
		if (!$name) {
			$name = '(untitled venue)';
		}
		
		if (User::isAdmin()) {
			$uo = User::get($postArray['user_id']);
			if (db::isError($uo)) {
				$e->add($uo);
			} else if (!$uo->isAdmin() && $uo->isBandMember() ) {
				$e->add("Invalid user. User must be a band member or an administrator.");
			}
		} else {
			$uo = User::getCurrent();
		}

		$user_id = $uo->getID();
		
		$r = @mysql_query("update Venues set country='{$country}', user_id = '{$user_id}', name='$name', address1='$address1', address2 = '$address2', city='$city', stateProvince = '$stateProvince', postalCode='$postalCode', directions='$directions' where ID = " . $this->ID);
		if ($r) {
			return Venue::get($this->ID);
		} else {
			return Error::MySQL();
		}
	}
	
	function add($postArray) {
		$db = new db;
		$uo = User::getCurrent();
		
		if (User::isAdmin()) {
			$uo = User::get($postArray['user_id']);
			if (db::isError($uo)) {
				$e->add($uo);
			} else if (!$uo->isAdmin() && $uo->isBandMember()) {
				$e->add("Invalid user. User must be a band member or an administrator.");
			}
		} else {
			$uo = User::getCurrent();
		}
		
		$user_id = $uo->getID();
		
		$name = $db->sanitize_to_db($postArray['name']);
		$address1 = $db->sanitize_to_db($postArray['address1']);
		$address2 = $db->sanitize_to_db($postArray['address2']);
		$city = $db->sanitize_to_db($postArray['city']);
		$stateProvince = $db->sanitize_to_db($postArray['stateProvince']);
		if ($stateProvince == "??") {
			$stateProvince = $db->sanitize_to_db($postArray['stateProvinceOther']);
		}
		$postalCode = $db->sanitize_to_db($postArray['postalCode']);
		$directions = $db->sanitize_to_db($postArray['directions']);
		$country = $db->sanitize_to_db($postArray['country']);
		$country = ($country == null) ? VENUE_DEFAULT_COUNTRY : $country;
		if (!$name) {
			$name = '(untitled venue)';
		}
		
		$r = mysql_query("insert into Venues (user_id, country, name, address1, address2, city, stateProvince, postalCode, directions, is_active) values ('$user_id', '$country', '$name', '$address1', '$address2', '$city', '$stateProvince', '$postalCode', '$directions'," . DEFAULT_ACTIVE . ")");	
		if ($r) {
			return Venue::get(mysql_insert_id());
		} else {
			return Error::MySQL();
		}
	}

}
?>